package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.event.SceneEvent;
	import engine.flag.GameDefine;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.utils.FilterUtil;

	/**
	 * 
	 * @author wanghb
	 * Buffer图标显示，根椐人物绑定属性来显示当前所拥有的Buffer属性
	 */	
	public class HortationUI extends Sprite
	{
		private var _bindDepotIndex	:int	=	-1;
		private var bindIndex			:int	=	-1;
		
		private var _bindManage		:BindManage;
		
		private var _model:ModelLocator;
		
		private var _iconFormat		:String;
		
		//对齐方式__CONVICTION:坚向___PORTRAIT:纵向
		public static var CONVICTION	:String	=	"conviction";
		public static var PORTRAIT	:String	=	"portrait";
		
		//图片存放数组
		private var iconArray			:Array;
		private var inspireArr			:Array	=	[];
		private var SPBonutArr			:Array	=	[];
		private var mainArr			:Array	=	[];
			
		//图片间的距离
		private var _picWidth			:Number	=	30;
		private var _spaceBetween		:Number	=	5;
		
		//当前对像的开始时间和持续时间
		private var starTime:uint;
		private var timeLong:uint;
		private var targetName:String;
		private var targetIntor:String;
		
		//判断鼠标是否在此界面操作	
		private var _buffBool			:Boolean =	false ;
		public function HortationUI(popupUI:Boolean=true)
		{
			init();			
		}


		/**
		 * 当完成初始化工作之后调用 
		 */	
		private function init():void
		{
			iconArray	=	new Array();
			_bindManage	=	BindManage.getInstance();
			_model = ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME),onOpen);
			_model.addEventListener(SceneEvent.SceneLoaded,bind);
			_iconFormat	=	HortationUI.PORTRAIT
			
		}
		/**
		 *设置活动图标的排列方式 
		 * conviction:坚向————portrait:纵向
		 * @param val
		 * 
		 */		
		public function set iconFormat(val:String):void
		{
			_iconFormat	=	val;
			iconCompositor();
		}
		
		/**
		 * 图标的宽度，因为加载时无法获得图片的高，所以要提前设定 
		 * @param value
		 * 
		 */		
		public function set picWidth(value:Number):void
		{
			_picWidth = value;
			iconCompositor();
		}
		/**
		 *设置活动图标的间据 
		 * @param val
		 * 
		 */		
		public function set spaceBetween(val:Number):void
		{
			_spaceBetween	=	val;
			iconCompositor();
		}

		
		private function onOpen(e:CustomMsgEvent):void
		{
			if(_buffBool == false)return;
			
			/****
			 * timeLong buf时长
			 * starTime Buf使用时间
			 * */

			if(int(timeLong)>(e.pList[0]-starTime))
			{
				
				var count:int = e.pList[0]-starTime;
				var time:int =int(timeLong)-(e.pList[0]-starTime);
				
				var isHours:uint=(time)/3600 ; 
				var isMine:int  = time%3600 ; 
	//			var Hours:uint = timeLong/1000/3600 ; 
				var strTime:String 	=""
				if(isHours>0)
				{
					var _timeCount:int =0; 
					if(isMine/60>1)
					{
						_timeCount=isMine/60
					}else
					{
						_timeCount=1
					}
					if(_timeCount==0)
					{
						strTime=StaticIni.getSwfLangStrVar("UIString_1000498",[isHours,00]);
					}else
					{
						strTime=StaticIni.getSwfLangStrVar("UIString_1000498",[isHours,_timeCount]);
					}
					
					
				}else
				{
					var timeCount:int =0; 
					if(time/60>1)
					{
						timeCount=time/60
					}else
					{
						timeCount=1
					}
					strTime = StaticIni.getSwfLangStrVar("UIString_1000501",[timeCount]);
				}
				TipsMainLogic.instance.showString((StaticIni.getSwfLangStr(targetIntor)+strTime).toString());
			}
			
//			var time_1:int=PlayCDTimerLogic.getInstance().getTimerStr(time,false) as int;
//			var timeLength:int=PlayCDTimerLogic.getInstance().getTimerStr(int(timeLong),false) as int;
			
//			if(timeLength>time_1)
//			{
//				var strTime:String 	=	StaticIni.getSwfLangStrVar("UIString_1000498",[timeLength-time_1]);
//				TipsManage.showTips(strTime);
//			}
//				
			
			
//			date.fstarTime - timeLong
//			var serverArray:Array	=	new Array();	
//			serverArray				=	e.pList.splice(",");
//			var serverTimer:Date	=	new Date(serverArray[0],serverArray[1]-1,serverArray[2],serverArray[3],serverArray[4],serverArray[5]);
//			TipsManage.showTips(StaticIni.getSwfLangStrVar("UIStr_1000598",[targetName,targetIntor,countTime(e.pList[0])]));
		}
		
		/**
		 *计算剩余时间 
		 */		
		private function countTime(val:uint):String
		{
			var RTimer:String	=	"";
			
			var allTime:uint	=	starTime	+	timeLong;
			var nowTime:uint	=	val;
			if(nowTime	>	allTime){
				RTimer	=	"00";
			}
			else{
				var resTime:uint	=	allTime	-	nowTime;
				if(resTime < 60){
					RTimer	=	StaticIni.getSwfLangStrVar("UIStr_1000795",["00",resTime]);
				}else{
					var tempM:int	=	Math.floor(resTime/60);
					var tempS:int	=	resTime - tempM * 60;
//					RTimer				=	RTimer	+	String(Math.floor(resTime/60));	
					RTimer			=	RTimer	=	StaticIni.getSwfLangStrVar("UIStr_1000795",[tempM,tempS]);
				}
					
			}
			return RTimer;
		}
		/**
		 * 数据绑定
		 */
		public  function bind(e:SceneEvent):void
		{
			var		roleObj:SceneObject	=	_model.scene.Role as SceneObject;
			if(_bindDepotIndex 	!= -1){
				_bindDepotIndex	=	_bindManage.UnbindProp(roleObj,"TimeBuffers","onDePositItem");
			}
			_bindDepotIndex	=	_bindManage.BindProp(roleObj,"TimeBuffers",this,"onDePositItem");
			_model.removeEventListener(SceneEvent.SceneLoaded,bind);
			if(bindIndex 	!= -1){
				bindIndex	=	_bindManage.UnbindProp(roleObj,"ExciteTimes","onInspire");
			}
			bindIndex	=	_bindManage.BindProp(roleObj,"ExciteTimes",this,"onInspire");
			if(bindIndex 	!= -1){
				bindIndex	=	_bindManage.UnbindProp(roleObj,"WorldBossExciteTimes","onInspire");
			}
			bindIndex	=	_bindManage.BindProp(roleObj,"WorldBossExciteTimes",this,"onInspire");
			if(bindIndex 	!= -1){
				bindIndex	=	_bindManage.UnbindProp(roleObj,"SPBonut","onSPBonut");
			}
			bindIndex	=	_bindManage.BindProp(roleObj,"SPBonut",this,"onSPBonut");
		}
		
		public function set onInspire(val:int):void
		{
			clearInspireArr();
			if(val>0)
			{
				var img:NxClip		=	new NxClip();
				img.defaultImage 	= null;
				img.data.HandleProperties("Type"				,10);
				img.data.HandleProperties("Level"				,val);
				img.source 			= 	"res/png/buffpic.png";
				inspireArr.push(img);
				addMouseEvent(img);	
				this.addChild(img);
			}
			iconCompositor();
		}
		
		public function set onSPBonut(val:int):void
		{
			clearSPBonutArr();
			if(val>0)
			{
				var img:NxClip		=	new NxClip();
				img.defaultImage 	= null;
				img.data.HandleProperties("Type"				,11);
				img.data.HandleProperties("Num"				,val);
				img.source 			= 	"res/png/buffpic.png";
				SPBonutArr.push(img);
				addMouseEvent(img);	
				this.addChild(img);
			}
			iconCompositor();
		}
		
		/**
		 * 绑定触发事件 
		 * @param val-------------------Toplayer 上面的bugff
		 * 
		 */		
		public function set onDePositItem(val:String):void
		{	
			clearIconArr();
		
			
			if(GameData.SceneType==GameDefine.SceneType_WorldBossScene) WorldBossBloodUI.getInstance().hideBuffers(false);
			if(GameData.SceneType==GameDefine.SceneType_FactionBossScene) FactionBossUI.getInstance().hideBuffers(false);
			
			if(val == "")
			{
				return;
			}
			var bufferArray:Array	=	val.split(",");
			
			for(var i:int = 0; i < bufferArray.length; i++)
			{
				if(bufferArray[i] != "" && bufferArray[i] != null)
				{
					var tempArray:Array			=	String(bufferArray[i]).split("|");
					
					var iconType:int			=	tempArray[0];
					if(iconType == 2 || iconType == 4){
						var _configID:String		=	tempArray[1];
						var _addTime:String			=	tempArray[2]
						var _bufferLifeTime:String	=	tempArray[3]
						var _icon:String			=	tempArray[4]
						
						var tempAddTime			:Number	=	Number(_addTime);
						var tempBufferLifeTime	:Number	=	Number(_bufferLifeTime);
						var tempIcon			:String	=	_icon
						var tempName			:String	=	StaticIni.getSwfLangStr(_configID);
						var tempIntor			:String	=	StaticIni.getSwfLangStr("BufferDes_"+_configID);
						/**
						 *tempAddTime 使用时间
						 * 
						 * tempBufferLifeTime服务器时间
						 * 
						 * tempIntor 字符串
						 * 
						 * tempIcon图片 ID
						 * 
						 * BUF 名字
						 * 
						 * iconType 类型
						 * **/
						addImageItem(tempAddTime,tempBufferLifeTime,tempIntor,"buff_"+_icon.toString(),tempName,iconType);
					}else if(iconType == 3)
					{
						var strType:String	=	tempArray[1];
						var strNum:Number	=	tempArray[2];
						var strIcon:String	=	tempArray[3];
						HMImageItem(strType,strNum,strIcon);
					}else if (iconType == 5)
					{
						var _confiID:String = tempArray[1]//configID 
							
						var _num:uint  =  uint(tempArray[2]);//次数
						
						var addition:Number = Number(tempArray[3])// 加成 现在100%
						
						var _ico:String = String(tempArray[4]) ; //图片
						
						addImage("buffpic",iconType,_num,addition); //
					}else if (iconType == 6)
					{
						var iconId:String	=	tempArray[1];
						worldBoss_addImage("buffpic",iconType,iconId);
					}else if (iconType == 7)
					{
						/**使用物品BUF**/
//						worldBoss_addImage("buffpic",iconType,iconId);
						
					}
				}	
			}
			iconCompositor();

		}
		
		/**
		 * 世界boss神符
		 * */
		private function worldBoss_addImage(icon:String,type:int,configId:String):void
		{
			
			var img:NxClip	=	new NxClip();
			img.defaultImage = null;
			img.data.HandleProperties("Type"				,type);
			img.data.HandleProperties("ConfigID"			,configId);
			
			img.source 		= 	"res/png/"+icon+".png";
			iconArray.push(img);
			this.addChild(img);
			addMouseEvent(img);	
		}
		
		/**
		 * 上香Buffer图标
		 * */
		private function addImage(icon:String,iconType:int,num:int,addition:Number):void
		{
						
			var img:NxClip	=	new NxClip();
			img.defaultImage = null;
			img.data.HandleProperties("Type"				,iconType);
			img.data.HandleProperties("Num"					,num);
			img.data.HandleProperties("Addition"			,addition);
			
			img.source 		= 	"res/png/"+icon+".png";
			iconArray.push(img);
			addMouseEvent(img);	
			this.addChild(img);
		}
		/**
		 * 添加一个Buffer图标
		 * @param AddTime		
		 * @param BufferLifeTime
		 * @param BufferIntor
		 * @param icon
		 * @param name
		 * 
		 */		
		private function addImageItem(AddTime:Number,BufferLifeTime:Number,BufferIntor:String,icon:String,name:String,iconType:int):void
		{
			var img:NxClip	=	new NxClip();	//true,null,ResManager.newResClass("NotFound")
			img.defaultImage = null;
			img.data.HandleProperties("Type"				,iconType);
			img.data.HandleProperties("AddTime"			,AddTime);
			img.data.HandleProperties("BufferLifeTime"	,BufferLifeTime/1000);
			img.data.HandleProperties("BufferIntor"		,BufferIntor);
			img.source 		= 	"res/png/"+icon+".png";			
			img.name		=	name;
			img.buttonMode	=	true;
			iconArray.push(img);
			addMouseEvent(img);	
			this.addChild(img);
		}
		/**
		 *添加血条图标显示  
		 * @param name
		 * @param maxNum
		 * @param icon
		 * 
		 */
		/**
		 * 添加血条图标显示 
		 */		
		private function HMImageItem(name:String,maxNum:Number,icon:String):void
		{
			var img:NxClip	=	new NxClip();	//true,null,ResManager.newResClass("NotFound")
			img.defaultImage = null;
			img.data.HandleProperties("Type"				,3);
			img.data.HandleProperties("Name"				,name);
			img.data.HandleProperties("Max"					,maxNum);
			img.source 		= 	"res/items/"+icon+".jpg";			
			img.name		=	name;
			iconArray.push(img);
			addMouseEvent(img);	
			this.addChild(img);
		}
		/**
		 * 删除当前所有的Buffer图标 
		 * 
		 */		
		private function clearIconArr():void
		{			
			if((iconArray!=null)	&&	(iconArray.length	>	0))
			{
				for(var i:int = 0;	i < iconArray.length; i++)
				{
					var dellMc:NxClip	=	iconArray[i] as NxClip;
					delMouseEvent(dellMc);
					dellMc.filters = null;
					this.removeChild(dellMc);
				}
				iconArray	=	[];
			}
		}
		
		private function clearInspireArr():void
		{			
			if((inspireArr!=null)	&&	(inspireArr.length	>	0))
			{
				for(var i:int = 0;	i < inspireArr.length; i++)
				{
					var dellMc:NxClip	=	inspireArr[i] as NxClip;
					delMouseEvent(dellMc);
					dellMc.filters = null;
					this.removeChild(dellMc);
				}
				inspireArr	=	[];
			}
		}
		
		private function clearSPBonutArr():void
		{			
			if((SPBonutArr!=null)	&&	(SPBonutArr.length	>	0))
			{
				for(var i:int = 0;	i < SPBonutArr.length; i++)
				{
					var dellMc:NxClip	=	SPBonutArr[i] as NxClip;
					delMouseEvent(dellMc);
					dellMc.filters = null;
					this.removeChild(dellMc);
				}
				SPBonutArr	=	[];
			}
		}
		
		/**
		 *活动内容图标的鼠标事件
		 * @param mc
		 */		
		private function addMouseEvent(mc:NxClip):void
		{
			mc.addEventListener(MouseEvent.ROLL_OVER,	iconOverHandle);
			mc.addEventListener(MouseEvent.ROLL_OUT,	iconOutHandle);
		}
		
		private function delMouseEvent(mc:NxClip):void
		{
			mc.removeEventListener(MouseEvent.ROLL_OVER,	iconOverHandle);
			mc.removeEventListener(MouseEvent.ROLL_OUT,		iconOutHandle);
		}
		
		private function iconOverHandle(e:MouseEvent):void
		{
			_buffBool			=	true;
			var showStr:String ="";
			var iconData:Object	=(e.target as NxClip).data
			if(iconData.GetProperties("Type") == 3){
				var tipStr:String	=	"";
				var maxNum:Number	=	iconData.GetProperties("Max");
				
				if(iconData.GetProperties("Name") == "HP"){
					
					showStr	=	StaticIni.getSwfLangStrVar("UIStr_1001052",[maxNum])
				}else if(iconData.GetProperties("Name") == "MP"){
					
					showStr	=	StaticIni.getSwfLangStrVar("UIStr_1001053",[maxNum])
				}
			}
			else if(iconData.GetProperties("Type") == 2)
			{
				
				starTime	=	(e.target as NxClip).data.GetProperties("AddTime");		
				timeLong	=	(e.target as NxClip).data.GetProperties("BufferLifeTime");//
				targetIntor	=	(e.target as NxClip).data.GetProperties("BufferIntor");
				targetName	=	(e.target as NxClip).name;
				
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);		
			}
			else if(iconData.GetProperties("Type") == 4)
			{
				var allTimeStr:String	=	"";
				targetIntor	=	(e.target as NxClip).data.GetProperties("BufferIntor");
				targetName	=	(e.target as NxClip).name;
				showStr	=	targetName + "\n" +targetIntor ;
			}else if (iconData.GetProperties("Type") == 5)
			{
				var  num:int = iconData.GetProperties("Num");	
				var addition:Number =  iconData.GetProperties("Addition");	
				var value:int= addition*100 ;
				showStr	=	StaticIni.getSwfLangStrVar("UIString_1000187",[num,value]);
			}
			else if (iconData.GetProperties("Type") == 6)
			{
				showStr	=	StaticIni.getSwfLangStr("BufferDes_"+iconData.GetProperties("ConfigID"));
			}
			else if (iconData.GetProperties("Type") == 10)
			{
				showStr	=	StaticIni.getSwfLangStrVar("BufferDes_83000007",[iconData.GetProperties("Level")*20]);
			}
			else if (iconData.GetProperties("Type") == 11)
			{
				showStr=	StaticIni.getSwfLangStrVar("UIString_1000313",[iconData.GetProperties("Num")]);
			}else if (iconData.GetProperties("Type") == 7)
			{
				/**使用物品BUF**/
//				var _BuffTip:String	=	StaticIni.getSwfLangStrVar("UIString_1000498",[iconData.GetProperties("ConfigID")]);
//				TipsManage.showTips(_BuffTip);
				
			}
			TipsMainLogic.instance.showString(showStr);

		}
		
		private function iconOutHandle(e:MouseEvent):void
		{
			_buffBool			=	false;
			TipsMainLogic.instance.hide();
		}
		
		/**
		 *对添加的图标进行排列 
		 */		
		private function iconCompositor():void
		{
			if(inspireArr.length!=0 && SPBonutArr.length!=0)
			{
				mainArr	=	SPBonutArr.concat(inspireArr.concat(iconArray));
			}
			else if(inspireArr.length!=0)
			{
				mainArr	=	inspireArr.concat(iconArray);
			}
			else if(SPBonutArr.length!=0)
			{
				mainArr	=	SPBonutArr.concat(iconArray);
			}
			else
			{
				mainArr	=	iconArray;
			}
			for(var i:int=0;	i<mainArr.length;	i++)
			{
				var changIco:NxClip	=	mainArr[i] as NxClip;
				
				if(_iconFormat	==	HortationUI.PORTRAIT)
				{
					if(changIco.data.CheckProperties("Type")&&changIco.data.GetProperties("Type")==6&&(!WorldBossBloodUI.getInstance().isHideBuffs 
						|| !FactionBossUI.getInstance().isHideBuffs ))
					{
						if(!WorldBossBloodUI.getInstance().isHideBuffs) WorldBossBloodUI.getInstance().hideBuffers(true);
						if(!FactionBossUI.getInstance().isHideBuffs) FactionBossUI.getInstance().hideBuffers(true);
						var rolePoint:Point	= GameData.Role.point;
						changIco.x = rolePoint.x;
						changIco.y = rolePoint.y;
						var xNum:int	=	(_picWidth + _spaceBetween) * i;
						TweenLite.to(changIco, 1, {x:xNum, y:0, overwrite:true, onComplete:function():void
						{
							changIco.filters = [FilterUtil.WhiteGlowFilter];
						}
						});
					}
					else
					{
						changIco.x	=	(_picWidth +	_spaceBetween) * i;
						changIco.y	=	0;
					}
				}
				else if(_iconFormat		==	HortationUI.CONVICTION)
				{
					changIco.y	=	(_picWidth +	_spaceBetween) * i;
					changIco.x	=	0;
				}
			}
		}
	}
}